﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Columns;
using Microsoft.Office.Core;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;

using DevExpress.XtraGrid.Views.Grid;

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;

using Excel;



namespace NNLInterface
{
    public partial class frmCustRpt : NNLInterface.frmBaseToolXC
    {
        int curDataType;
        private int dataType;
        private System.Data.DataTable dtExcelColMap = null;
        private System.Data.DataTable dtRpt;
        int rtpDataType;
        int monthRange;

        public frmCustRpt(int dataType)
        {
            InitializeComponent();
            curDataType = dataType;

        }

        private void frmCustWeekRpt_Load(object sender, EventArgs e)
        {
            this.SetToolVisible(common.ToolVisible.ImportData);
            this.SaveButtonVisibility = true;
            dtRpt = new System.Data.DataTable();
            SetDataSource();
            if (curDataType == 1)
            {
                this.Text = "产能分析报表(客户别)";
                this.ListGrid.Fields[0].Caption = "客户";
            }
            else
            {
                this.Text = "产能分析报表(部门别)";
                this.ListGrid.Fields[0].Caption = "部门";
            }

        }


        private void SetDataSource()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("ItemID", typeof(int));
            dt.Columns.Add("RptType", typeof(string));
            dt.Rows.Add(new object[] { 1, "按月统计" });
            dt.Rows.Add(new object[] { 2, "按周统计" });
            this.lokRptType.Properties.DataSource = dt;

            //取得系统时间
            string dtNow = (string)accessdb.GetField("select dbo.getshortdate(getdate())");
            this.dteFrom.EditValue = dtNow;

            this.logButtonVisibility = false;
            this.ImportButtonVisibility = false;
            this.CmdSearch.Caption = "报表生成";
        }

        private void frmCustRpt_ToolButtonClick(object sender, string e)
        {
            Cursor currentCursor = Cursor.Current;
            try
            {
                switch (e)
                {
                    case "search":
                        if (this.lokRptType.Text == "")
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("报表类型不能为空，请检查!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        if (this.dteTo.Text == "")
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("请输入截止日期!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.dteTo.Focus();
                            return;
                        }

                        Cursor.Current = Cursors.WaitCursor;

                        if (!CheckData()) return;

                        if (dtExcelColMap == null)
                            dtExcelColMap = accessdb.GetRecord("SELECT ecm.ColNo,ecm.ColNum FROM ExcelColMap ecm ").ADODataRst;

                        rtpDataType = Convert.ToInt32(lokRptType.Text);

                        string strDateFrom=this.dteFrom.Text;
                        string strDateTo=this.dteTo.Text;

                        string strDate="SELECT   DATEDIFF(M,'" + strDateFrom + "','" + strDateTo + "')";

                        monthRange = Convert.ToInt32(accessdb.GetField(strDate))+1;

                        //显示FC数据
                        string strSQL = "SELECT * FROM vie_ForeCastData vfcd";
                        System.Data.DataTable dtFc = accessdb.GetRecord(strSQL).ADODataRst;
                        common.SubShowList(dtFc, this.ListGridFc, this.ListViewFc, false);
                        common.subIniGrid(this.ListViewFc, common.enumGridStyle.ViewStyle);
                        common.GetResourceString(this.Name, this.ListViewFc);
                        this.ListGridFc.MenuManager = this.barmanager;
                        this.ListViewFc.BestFitColumns();


                        //显示Mo数据
                        strSQL = "SELECT * FROM vie_ErpNotCompleteMo vencm";
                        System.Data.DataTable dtMo = accessdb.GetRecord(strSQL).ADODataRst;
                        common.SubShowList(dtMo, this.ListGridMo, this.ListViewMo, false);
                        common.subIniGrid(this.ListViewMo, common.enumGridStyle.ViewStyle);
                        common.GetResourceString(this.Name, this.ListViewMo);
                        this.ListGridMo.MenuManager = this.barmanager;
                        this.ListViewMo.BestFitColumns();




                        //显示数据
                        //客户别
                        if (curDataType == 1)
                            dtRpt = (System.Data.DataTable)accessdb.ExecuteSP("pro_GetCustRptData", common.Choose.RetOneRecord, new object[] { this.lokRptType.EditValue, this.dteFrom.Text, this.dteTo.Text });
                        else //部门别
                        {
                            strSQL = "   SET XACT_ABORT ON  "
                                    + "   TRUNCATE TABLE CustTimes "
                                    + "   TRUNCATE TABLE DateRange "
                                    + "   BEGIN TRAN "
                                    + "   EXEC pro_GetDeptRptDataZ1 "
                                    + "   @rptType=" + Convert.ToInt16(this.lokRptType.EditValue).ToString() + ", "
                                    + "   @DateFrom='" + this.dteFrom.Text + "',"
                                    + "   @DateTo='" + this.dteTo.Text + "' "

                                    + "   EXEC pro_GetDeptRptDataZ2 "
                                    + "   @rptType=" + Convert.ToInt16(this.lokRptType.EditValue).ToString() + ", "
                                    + "   @DateFrom='" + this.dteFrom.Text + "',"
                                    + "   @DateTo='" + this.dteTo.Text + "' "

                                    + "   EXEC pro_GetDeptRptDataZ3 "
                                    + "   @rptType=" + Convert.ToInt16(this.lokRptType.EditValue).ToString() + ", "
                                    + "   @DateFrom='" + this.dteFrom.Text + "',"
                                    + "   @DateTo='" + this.dteTo.Text + "' "

                                    + "   EXEC pro_GetDeptRptDataZ6 "
                                    + "   @rptType=" + Convert.ToInt16(this.lokRptType.EditValue).ToString() + ", "
                                    + "   @DateFrom='" + this.dteFrom.Text + "',"
                                    + "   @DateTo='" + this.dteTo.Text + "' "

                                    + "   EXEC pro_GetDeptRptDataZ7 "
                                    + "   @rptType=" + Convert.ToInt16(this.lokRptType.EditValue).ToString() + ", "
                                    + "   @DateFrom='" + this.dteFrom.Text + "',"
                                    + "   @DateTo='" + this.dteTo.Text + "' "

                                    + "   COMMIT TRAN ";
                            if (!accessdb.RsChange(strSQL)) return;
                            strSQL = "SELECT ct.CustNo, ct.TimesType, ct.DataType, ct.rptDate, Qty FROM   CustTimes ct ";
                            dtRpt = accessdb.GetRecord(strSQL).ADODataRst;

                        }

                        ListGrid.DataSource = dtRpt;
                        this.ListGrid.MenuManager = this.barmanager;

                        Cursor.Current = currentCursor;
                        break;

                    case "excel":

                         string fileName = common.ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls");
                         if (fileName != "")
                         {
                             ListGrid.ExportToXls(fileName);
                         }
                        if(curDataType == 2 &&  rtpDataType==1)
                        {
                            //需要再做处理
                          

                            Excel.Application xApp = new Excel.ApplicationClass();
                            xApp.Workbooks.Open(fileName, Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                            xApp.Visible = false;

                            
                            Excel.Workbook xBook = xApp.Workbooks[1];
                            xApp.Application.DisplayAlerts = false;   //关闭提示 
                            
                            


                            Excel.Worksheet sheet = (Excel.Worksheet)xApp.ActiveSheet;// 获取当前工作表
                            Excel.Range range;

                            //取得课的个数
                            string strItem = common.GetCellData(xApp, sheet, 7, 3, false);

                            int startRow = 7;
                            
                            while (strItem!="")
                            {
                                startRow++;
                                strItem = common.GetCellData(xApp, sheet, startRow, 3, false);
                            }

                            int endRow = startRow-1;

                            int deptRange = (endRow - 3) / 3;

                            

                            //合并发工资后预计自离人力，辞职人数
                            for (int i = 0; i < 5; i++)
                            {
                                for (int jj = 0; jj < monthRange; jj++)
                                {
                                    range = sheet.get_Range(sheet.Cells[4 + i * 3, 10+jj*7], sheet.Cells[6+i*3, 10+jj*7]); //取得合并的区域 
                                    range.MergeCells = true;


                                    range = sheet.get_Range(sheet.Cells[4 + i * 3, 11+jj*7], sheet.Cells[6+i*3, 11+jj*7]); //取得合并的区域 
                                    range.MergeCells = true;
                                } 
                               
                            }

                            //管理课待分配人数
                            for (int jj = 0; jj < monthRange; jj++)
                            {
                                range = sheet.get_Range(sheet.Cells[4, 12 + jj * 7], sheet.Cells[endRow, 12 + jj * 7]); //取得合并的区域 
                                range.MergeCells = true;


                               
                            }

                            //写入表格尾部合计数据
                            range = sheet.get_Range(sheet.Cells[endRow + 1, 1], sheet.Cells[endRow + 1, 3]); //取得合并的区域 
                            range.MergeCells = true;

                            sheet.Cells[endRow + 1, 1] = "汇总(自动计算)";

                            range = sheet.get_Range(sheet.Cells[endRow + 2, 1], sheet.Cells[endRow + 2, 3]); //取得合并的区域 
                            range.MergeCells = true;

                            sheet.Cells[endRow + 2, 1] = "差异(自动计算)";

                            range = sheet.get_Range(sheet.Cells[endRow + 1, 4], sheet.Cells[endRow + 1, 6]); //取得合并的区域 
                            range.MergeCells = true;

                            range = sheet.get_Range(sheet.Cells[endRow + 2, 4], sheet.Cells[endRow + 2, 6]); //取得合并的区域 
                            range.MergeCells = true;

                            //画表格线
                            sheet.get_Range(sheet.Cells[endRow + 1, 1], sheet.Cells[endRow + 2, 5 + 7 * monthRange]).Borders.LineStyle = 1;
                            sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[endRow + 2, 5 + 7 * monthRange]).Borders[XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;//设置左边线加粗
                            sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[endRow + 2, 5 + 7 * monthRange]).Borders[XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;//设置上边线加粗
                            sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[endRow + 2, 5 + 7 * monthRange]).Borders[XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;//设置右边线加粗
                            sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[endRow + 2, 5 + 7 * monthRange]).Borders[XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;//设置下边线加粗

                           

                            //按月写入合计公式
                            for (int i = 0; i < monthRange; i++)
                            {
                                switch (i)
                                {
                                    case 0:
                                        //1.需求产能
                                        range = sheet.get_Range(sheet.Cells[19, 4], sheet.Cells[19, 4]);
                                        range.Formula = "=(D9+D12+D15+D18)/3600";

                                        //2.需求人力
                                        range = sheet.get_Range(sheet.Cells[19, 7], sheet.Cells[19, 7]);
                                        range.Formula = "=G9+G12+G15+G18";

                                        //3.现有人力
                                        range = sheet.get_Range(sheet.Cells[19, 8], sheet.Cells[19, 8]);
                                        range.Formula = "=H9+H12+H15+H18";

                                        //4.差异人力
                                        range = sheet.get_Range(sheet.Cells[19, 9], sheet.Cells[19, 9]);
                                        range.Formula = "=I9+I12+I15+I18";


                                        //5.预计自离人力
                                        range = sheet.get_Range(sheet.Cells[19, 10], sheet.Cells[19, 10]);
                                        range.Formula = "=SUM(J7:J18)";

                                        //6.辞职人数
                                        range = sheet.get_Range(sheet.Cells[19, 11], sheet.Cells[19, 11]);
                                        range.Formula = "=SUM(K7:K18)";

                                        //7.待分配人数
                                        range = sheet.get_Range(sheet.Cells[19, 12], sheet.Cells[19, 12]);
                                        range.Formula = "=L4";
                                        break;

                                    case 1:
                                        //1.需求产能
                                        range = sheet.get_Range(sheet.Cells[19, 13], sheet.Cells[19, 13]);
                                        range.Formula = "=(M9+M12+M15+M18)/3600";

                                        //2.需求人力
                                        range = sheet.get_Range(sheet.Cells[19, 14], sheet.Cells[19, 14]);
                                        range.Formula = "=N9+N12+N15+N18";

                                        //3.现有人力
                                        range = sheet.get_Range(sheet.Cells[19, 15], sheet.Cells[19, 15]);
                                        range.Formula = "=O9+O12+O15+O18";

                                        //4.差异人力
                                        range = sheet.get_Range(sheet.Cells[19, 16], sheet.Cells[19, 16]);
                                        range.Formula = "=P9+P12+P15+P18";


                                        //5.预计自离人力
                                        range = sheet.get_Range(sheet.Cells[19, 17], sheet.Cells[19, 17]);
                                        range.Formula = "=SUM(Q7:Q18)";

                                      

                                        //6.辞职人数
                                        range = sheet.get_Range(sheet.Cells[19, 18], sheet.Cells[19, 18]);
                                        range.Formula = "=SUM(R7:R18)";

                                        //7.待分配人数
                                        range = sheet.get_Range(sheet.Cells[19, 19], sheet.Cells[19, 19]);
                                        range.Formula = "=S4";
                                        break;

                                      case 2:
                                        //1.需求产能
                                        range = sheet.get_Range(sheet.Cells[19, 20], sheet.Cells[19, 20]);
                                        range.Formula = "=(T9+T12+T15+T18)/3600";

                                        //2.需求人力
                                        range = sheet.get_Range(sheet.Cells[19, 21], sheet.Cells[19, 21]);
                                        range.Formula = "=U9+U12+U15+U18";

                                        //3.现有人力
                                        range = sheet.get_Range(sheet.Cells[19, 22], sheet.Cells[19, 22]);
                                        range.Formula = "=V9+V12+V15+V18";

                                        //4.差异人力
                                        range = sheet.get_Range(sheet.Cells[19, 23], sheet.Cells[19, 23]);
                                        range.Formula = "=W9+W12+W15+W18";


                                        //5.预计自离人力
                                        range = sheet.get_Range(sheet.Cells[19, 24], sheet.Cells[19, 24]);
                                        range.Formula = "=SUM(X7:X18)";

                                      

                                        //6.辞职人数
                                        range = sheet.get_Range(sheet.Cells[19, 25], sheet.Cells[19, 25]);
                                        range.Formula = "=SUM(Y7:Y18)";

                                        //7.待分配人数
                                        range = sheet.get_Range(sheet.Cells[19, 26], sheet.Cells[19, 26]);
                                        range.Formula = "=Z4";
                                        break;

                                      case 3:
                                        //1.需求产能
                                        range = sheet.get_Range(sheet.Cells[19, 27], sheet.Cells[19, 27]);
                                        range.Formula = "=(AA9+AA12+AA15+AA18)/3600";

                                        //2.需求人力
                                        range = sheet.get_Range(sheet.Cells[19, 28], sheet.Cells[19, 28]);
                                        range.Formula = "=AB9+AB12+AB15+AB18";

                                        //3.现有人力
                                        range = sheet.get_Range(sheet.Cells[19, 29], sheet.Cells[19, 29]);
                                        range.Formula = "=AC9+AC12+AC5+AC18";

                                        //4.差异人力
                                        range = sheet.get_Range(sheet.Cells[19, 30], sheet.Cells[19, 30]);
                                        range.Formula = "=AD9+AD12+AD15+AD18";


                                        //5.预计自离人力
                                        range = sheet.get_Range(sheet.Cells[19, 31], sheet.Cells[19, 31]);
                                        range.Formula = "=SUM(AE7:AE18)";



                                        //6.辞职人数
                                        range = sheet.get_Range(sheet.Cells[19, 32], sheet.Cells[19, 32]);
                                        range.Formula = "=SUM(AF7:AF18)";

                                        //7.待分配人数
                                        range = sheet.get_Range(sheet.Cells[19, 33], sheet.Cells[19, 33]);
                                        range.Formula = "=AG4";
                                        break;

                                      case 4:
                                        //1.需求产能
                                        range = sheet.get_Range(sheet.Cells[19, 34], sheet.Cells[19, 34]);
                                        range.Formula = "=(AH9+AH12+AH15+AH18)/3600";

                                        //2.需求人力
                                        range = sheet.get_Range(sheet.Cells[19, 35], sheet.Cells[19, 35]);
                                        range.Formula = "=AI9+AI12+AI15+AI18";

                                        //3.现有人力
                                        range = sheet.get_Range(sheet.Cells[19, 36], sheet.Cells[19, 36]);
                                        range.Formula = "=AJ9+AJ12+AJ5+AJ18";

                                        //4.差异人力
                                        range = sheet.get_Range(sheet.Cells[19, 37], sheet.Cells[19, 37]);
                                        range.Formula = "=AK9+AK12+AK15+AK18";


                                        //5.预计自离人力
                                        range = sheet.get_Range(sheet.Cells[19, 38], sheet.Cells[19, 38]);
                                        range.Formula = "=SUM(AL7:AL18)";



                                        //6.辞职人数
                                        range = sheet.get_Range(sheet.Cells[19, 39], sheet.Cells[19, 39]);
                                        range.Formula = "=SUM(AM7:AM18)";

                                        //7.待分配人数
                                        range = sheet.get_Range(sheet.Cells[19, 40], sheet.Cells[19, 40]);
                                        range.Formula = "=AN4";
                                        break;

                                      case 5:
                                        //1.需求产能
                                        range = sheet.get_Range(sheet.Cells[19, 41], sheet.Cells[19, 41]);
                                        range.Formula = "=(AO9+AO12+AO15+AO18)/3600";

                                        //2.需求人力
                                        range = sheet.get_Range(sheet.Cells[19, 42], sheet.Cells[19, 42]);
                                        range.Formula = "=AP9+AP12+AP15+AP18";

                                        //3.现有人力
                                        range = sheet.get_Range(sheet.Cells[19, 43], sheet.Cells[19, 43]);
                                        range.Formula = "=AQ9+AQ12+AQ5+AQ18";

                                        //4.差异人力
                                        range = sheet.get_Range(sheet.Cells[19, 44], sheet.Cells[19, 44]);
                                        range.Formula = "=AR9+AR12+AR15+AR18";


                                        //5.预计自离人力
                                        range = sheet.get_Range(sheet.Cells[19, 45], sheet.Cells[19, 45]);
                                        range.Formula = "=SUM(AS7:AS18)";



                                        //6.辞职人数
                                        range = sheet.get_Range(sheet.Cells[19, 46], sheet.Cells[19, 46]);
                                        range.Formula = "=SUM(AT7:AT18)";

                                        //7.待分配人数
                                        range = sheet.get_Range(sheet.Cells[19, 47], sheet.Cells[19, 47]);
                                        range.Formula = "=AU4";
                                        break;
                                }
                                


                               
                            }


                            xApp.Application.DisplayAlerts = true;     //恢复提示

                            xBook.Save();
                            //xApp.Quit(); // 退出 Excel
                            KillExcelProcess.Kill(xApp);//调用kill当前excel进程 

                        }
                        DevExpress.XtraEditors.XtraMessageBox.Show("报表数据已成功导出!" + "\r\n文件名：" + fileName, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);

                        break;

                    case "exit":
                        this.Dispose();
                        break;

                    case "save":
                        if (this.dtRpt.Rows.Count <= 0)
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("没有数据可供保存!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要保存当前数据吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;

                        Cursor.Current = Cursors.WaitCursor;
                        int dataKind = 0;
                        //如果是客户别按月
                        if (curDataType == 1 && dataType == 1)
                            dataKind = 1;
                        else if (curDataType == 1 && dataType == 2)
                            dataKind = 2;
                        else if (curDataType == 2 && dataType == 1)
                            dataKind = 3;
                        else
                            dataKind = 4;
                        object obj = null;
                        obj = accessdb.ExecuteSP("pro_SaveCapabilityRpt", common.Choose.OnlyExecSp, new object[] { dataKind });
                        Cursor.Current = currentCursor;

                        //保存数据
                        if (obj != null)
                            DevExpress.XtraEditors.XtraMessageBox.Show("数据已成功保存!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);

                        //显示历史数据
                        ShowLogData(dataKind);

                        break;
                    default:
                        {
                            break;
                        }
                }


            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private string GetExcelColString(int formulaStart)
        {
            DataRow[] dr = dtExcelColMap.Select("ColNum=" + formulaStart.ToString());
            if (dr.Length >= 1)
                return (string)dr[0][0];
            else
                return "1";

        }

        private bool CheckData()
        {

            //间隔只能是6个月
            int dateRange = Convert.ToInt32(accessdb.GetField("select datediff(m,'" + this.dteFrom.Text.Substring(0, 7) + "-01','" + this.dteTo.Text.Substring(0, 7) + "-01')"));
            if (dateRange > 6)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("日期间隔不能超过6个月", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }


            if (string.Compare(this.dteTo.Text.Substring(0, 7), this.dteFrom.Text.Substring(0, 7)) < 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("截止日期不能小于起始日期!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            //int checkData = Convert.ToInt16(common.GetLanguageWord(this.Name, "checkData"));

            bool checkData = common.sysParameter.ShowError;

            if (checkData)
            {
                //如果品目的工时数据没有，则需y要提示
                string strSQL = "SELECT DISTINCT itemcode FROM vie_ItemNotIn vini \n"
                               + " WHERE vini.RDate>='" + this.dteFrom.Text + "'"
                               + " AND vini.RDate<='" + this.dteTo.Text + "'";
                System.Data.DataTable dtItemCode = accessdb.GetRecord(strSQL).ADODataRst;

                if (dtItemCode.Rows.Count >= 1)
                {
                    frmInformation frminfitemcode = new frmInformation(dtItemCode, "品目未设定产能信息,通过【标准产能基础表】进行数据的维护", "ItemCode");
                    frminfitemcode.ShowDialog();
                    return false;
                }
            }

            return true;
        }




        private string  subExportXLS()
        {
            try
            {
                string fileName = common.ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls");
                if (fileName != "")
                {
                    ListGrid.ExportToXls(fileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("报表数据已成功导出!" + "\r\n文件名：" + fileName, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return fileName;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return "";
            }

        }

        private void lokRptType_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lokRptType.Text != "")
            {
                this.txtRptType.Text = this.lokRptType.GetColumnValue(this.lokRptType.Properties.Columns["RptType"]).ToString();
                dataType = Convert.ToInt16(this.lokRptType.Text);

                //显示历史数据
                ShowLogData(Convert.ToInt32(this.lokRptType.Text));
            }
            else
            {
                this.txtRptType.Text = "";
            }
        }

        private void ShowLogData(int dataType)
        {
            int dataKind = 0;

            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            //如果是客户别按月
            if (curDataType == 1 && dataType == 1)
                dataKind = 1;
            else if (curDataType == 1 && dataType == 2)
                dataKind = 2;
            else if (curDataType == 2 && dataType == 1)
                dataKind = 3;
            else
                dataKind = 4;

            string strSQL = "SELECT * FROM CustTimesLog ctl WHERE ctl.DataKind=" + dataKind.ToString();
          System.Data.DataTable dtData = accessdb.GetRecord(strSQL).ADODataRst;
            this.ListGridLog.DataSource = dtData;
            common.SubShowList(dtData, this.ListGridLog, this.ListViewLog, false);
            common.subIniGrid(this.ListViewLog, common.enumGridStyle.ViewStyle);
            common.GetResourceString(this.Name, this.ListViewLog);
            this.ListGridLog.MenuManager = this.barmanager;
            this.ListViewLog.BestFitColumns();

            Cursor.Current = currentCursor;

        }


        private void ListView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "组" + e.RowHandle.ToString();
                }
            }
        }

        private void ListViewFc_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Column)
            {
                DevExpress.XtraGrid.Menu.GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
                if (menu.Column != null)
                {
                    //Adding new items 
                    DevExpress.XtraGrid.Views.Grid.GridView listView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
                    menu.Column.Tag = listView.Tag;
                    menu.Items.Add(CreateCheckItem("导出EXcel", menu.Column, FixedStyle.None,
                      imgList.Images[0]));

                   
                }

            }
        }

        //Create a menu item 
        DXMenuItem CreateCheckItem(string caption, GridColumn column, FixedStyle style, Image image)
        {
            DXMenuItem item = new DXMenuItem(caption,new EventHandler(OnExportClick),image );
            
            item.Tag =column.Tag;
            return item;
        }

        //Menu item click handler 
        void OnExportClick(object sender, EventArgs e)
        {
            DXMenuItem item = sender as DXMenuItem;
            DevExpress.XtraGrid.Views.Grid.GridView listView=new DevExpress.XtraGrid.Views.Grid.GridView();

            switch (item.Tag.ToString())
            {
                case "fc":
                    listView = ListViewFc;
                    break;
                case "mo":
                    listView = ListViewMo;
                    break;
                case "log":
                    listView = ListViewLog;
                    break;

            }
            if (!common.CheckExistsData(listView)) return;
            common.ExportExcel(listView);
            return;
        }

     





    }

    //The class that stores menu specific information 
    class MenuInfo
    {
        public MenuInfo(GridColumn column, FixedStyle style)
        {
            this.Column = column;
            this.Style = style;
        }
        public FixedStyle Style;
        public GridColumn Column;
    }
}